<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\controllers;

use app\models\MessageRecorder;

class MessageController extends BaseController{

    public function actionAdd(){
        $post = \Yii::$app->request->post();
        $id = $post['MessageRecorder']['id'];
        if($id){
            $model = MessageRecorder::find()->where('id=:id',[':id' => $id])->one();
            if(!$model){
                $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
                return json_encode($result);
            }
            if($model->message_cate!=$post['MessageRecorder']['message_cate']){
                $result = ['result'=>0,'data'=>'修改不能选择分类'];
                return json_encode($result);
            }
        }else{
            $model = new MessageRecorder();
            $model->addtime = time();
            //判断是否有重复分类
            $cate = intval($post['MessageRecorder']['message_cate']);
            $is_cate = MessageRecorder::find()->where('message_cate=:cate',[':cate' => $cate])->count();
            if($is_cate){
                $result = ['result'=>0,'data'=>'已经存在当前分类，请删除当前分类内容后重新添加'];
                return json_encode($result);
            }
        }
        $model->updatetime = time();
        $result = $model->add($post);
        return json_encode($result);
    }


    public function actionList(){
        $list = MessageRecorder::find();
        $list = $list->orderBy('addtime DESC')->asArray()->all();
        foreach ($list as $k=>$v){
            $list[$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
            $list[$k]['updatetime'] = date('Y-m-d H:i:s',$v['updatetime']);
            switch($v['message_cate']){
                case 0:
                    $cate_name = "无分类";
                    break;
                case 1:
                    $cate_name = "订单配送提示";
                    break;
                case 2:
                    $cate_name = "商品送达提示";
                    break;
                case 3:
                    $cate_name = "优惠券过期提示";
                    break;
                default:
                    $cate_name = "";
            }
            $list[$k]['cate_name'] = $cate_name;
        }
        $result = ['result'=>1,'data'=>$list];
        return json_encode($result);
    }


    public function actionOne(){
        $id = \Yii::$app->request->post('id');
        $model = MessageRecorder::find()->where('id=:id',[':id' => $id])->asArray()->one();
        if(empty($model)){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model['message_content'] = json_decode($model['message_content'],true);
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }


    public function actionDel(){
        $id = \Yii::$app->request->post('id');
        $model = MessageRecorder::find()->where('id=:id',[':id' => $id])->one();
        if(!$model){
            $result =['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result);
        }
        $model->delete();
        $result = ["result"=>1,'data'=>'操作成功'];
        return json_encode($result);
    }


}
